/**
 * @Author: lixiaojun
 * @Date: 2020/5/20 9:50
 */
package main

import (
	"context"
	"strconv"

	"git.900sui.cn/kc/kcgin"
	"git.900sui.cn/kc/kcgin/logs"
	"git.900sui.cn/kc/rabbitmq"
	"git.900sui.cn/kc/rpcinterface/client/dataVisualization"
	"git.900sui.cn/kc/rpcinterface/client/elastic/bus"
	"git.900sui.cn/kc/rpcinterface/client/risk"

	"git.900sui.cn/kc/rpcinterface/client/comtreeData"
	comtreeData2 "git.900sui.cn/kc/rpcinterface/interface/comtreeData"
	"github.com/streadway/amqp"
)

func main() {
	rpcShop := new(bus.Shop).Init()
	rpcVisualization := new(dataVisualization.Bus).Init()

	rpcbbaa := new(risk.BusBasicAccumulative).Init()
	defer rpcbbaa.Close()

	rpcMerchantShop := new(comtreeData.MerchantShop).Init()
	defer rpcMerchantShop.Close()
	logs.Info("Environment Variable:MSF_ENV:", kcgin.KcConfig.RunMode)
	err := rabbitmq.Rabbitmq.Consume(kcgin.AppConfig.String("queue.setShop"), func(msg amqp.Delivery) {
		// 通过消息队列将分店基本信息添加/更新到ES
		shopId, err := strconv.Atoi(string(msg.Body))
		logs.Info("shopId:", shopId)
		if err != nil {
			return
		}

		reply := false
		if err := rpcShop.SetShop(context.Background(), &shopId, &reply); err != nil {
			logs.Error("添加分店基本信息到Es失败，失败信息：\n", shopId, err.Error())
		}
		//监管可视化-分店信息
		if err := rpcVisualization.CreateOrUpdateBusShopInfo(context.Background(), &shopId, &reply); err != nil {
			logs.Error("添加分店基本信息到监管可视化失败，失败信息：\n", shopId, err.Error())
		}
		// 添加 预付卡风险管理系统--风控系统商户ID 门店总数量统计
		if err := rpcbbaa.ShopNumRpc(context.Background(), shopId, &reply); err != nil {
			logs.Error("添加分店基本信息到风险管理系统，失败信息：\n", shopId, err.Error())
			return
		}
		//门店入驻-comtreeData
		if err := rpcMerchantShop.AddMerchantShopRpc(context.Background(), &comtreeData2.ArgsMerchantShop{ShopId: shopId}, &reply); err != nil {
			logs.Error("添加门店信息到comtreeData，失败信息：\n", shopId, err.Error())
		}
	})
	if err != nil {
		panic(err)
	}
}
